﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GatewayInspect.Common;

namespace GatewayInspect.Modules
{
    public partial class BlackListModule : BaseModule
    {
        public BlackListModule()
        {
            InitializeComponent();
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();
            this.tsBtnModify.Visible = true;
            this.tsBtnImportExcel.Visible = true;
            this.tsBtnDownTemplate.Visible = true;
            this.tsBtnSave.Visible = true;
            this.toolStripSeparator1.Visible = true;
            this.tsBtnDelete.Visible = true;
            this.tsBtnNew.Visible = true;
            this.tsBtnSave.Visible = true;
            //控制只有实华公司用户可以删除黑名单
            this.tsBtnDelete.Enabled = (Session.CurrentOrgCode == "01");
        }

        protected override void OnLoadModule()
        {
            base.OnLoadModule();

            string strSql = "select a.*,b.ORGNAME from T_TRUCK_BLACKLIST a left join T_SYS_ORG b on a.ORGCODE=b.ORGCODE where a.ISDEL = '0'";
            if (!string.IsNullOrWhiteSpace(tbxDriverID.Text))
            {
                strSql += " and DRIVERID like '%" + tbxDriverID.Text + "%'";
            }
            if (!string.IsNullOrWhiteSpace(tbxTruckNo.Text))
            {
                strSql += " and TRUCKNO like '%" + tbxTruckNo.Text + "%'";
            }
            if (cmbOrg.SelectedValue != null)
            {
                strSql += " and a.ORGCODE = '" + cmbOrg.SelectedValue.ToString() + "'";
            }
            this.cdgvBlacklist.AutoGenerateColumns = false;
            this.cdgvBlacklist.DataSource = DataAccess.ExecuteDataTable(strSql);
        }

        protected override void OnNew()
        {
            base.OnNew();

            using (BlacklistAddForm frmAdd = new BlacklistAddForm())
            {
                if (frmAdd.ShowDialog() == DialogResult.OK)
                {
                    MessageBox.Show("添加黑名单成功！");
                    OnLoadModule();
                }
            }
        }

        protected override void OnDelete()
        {
            base.OnDelete();

            if (cdgvBlacklist.SelectedCells != null && cdgvBlacklist.SelectedCells.Count > 0)
            {
                DataGridViewRow row = cdgvBlacklist.SelectedCells[0].OwningRow;
                using (BlacklistDeleteForm frmDelete = new BlacklistDeleteForm(row.Cells["ID"].Value.ToString(),
                    row.Cells["TRUCKNO"].Value.ToString(), row.Cells["DRIVERID"].Value.ToString(), row.Cells["DRIVERNAME"].Value.ToString()))
                {
                    if (frmDelete.ShowDialog() == DialogResult.OK)
                    {
                        MessageBox.Show("删除成功！");
                        OnLoadModule();
                    }
                }
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            OnLoadModule();
        }

        private void btnHistory_Click(object sender, EventArgs e)
        {
            using (BlacklistHistoryForm frmHistory = new BlacklistHistoryForm())
            {
                frmHistory.ShowDialog();
            }
        }

        private void InitQueryFilter()
        {
            //设置库区
            string strSQL = "select ORGCODE, ORGNAME from T_SYS_ORG order by ORGCODE";
            DataTable dt = DataAccess.ExecuteDataTable(strSQL);
            if (dt != null && dt.Rows.Count > 0)
            {
                cmbOrg.DataSource = dt;
                cmbOrg.ValueMember = "ORGCODE";
                cmbOrg.DisplayMember = "ORGNAME";
                cmbOrg.SelectedValue = "";
            }
        }

        #region 读身份证

        private IDCradReader reader = null;

        protected override void OnInit()
        {
            base.OnInit();
            InitQueryFilter();
            BeginRead();//启动身份证读卡设备开始读卡 
        }

        protected override void OnModuleDestroyed()
        {
            base.OnModuleDestroyed();
            if (reader != null)
            {
                reader.AfterReadInfo -= new IDCradReader.AfterReadInfoHandler(reader_AfterReadInfo);
                reader.CheckHandler();//检查是否还有其他对象读卡
            }
        }

        private void BeginRead()
        {
            if (reader == null)
            {
                reader = IDCradReader.GetInstance();
                reader.AfterReadInfo += new IDCradReader.AfterReadInfoHandler(reader_AfterReadInfo);
            }
            reader.BeginRead();
        }

        private void reader_AfterReadInfo(CardInfo info)
        {
            if (IsActive && info != null)
            {
                this.tbxDriverID.Text = info.CardNo;
            }
        }

        #endregion
    }
}
